package com.cy.pj.aritmetic.controller;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ArithmeticController {
    @RequestMapping("doCompute/{n1}/{n2}")
    public String doCompute(@PathVariable Integer n1, @PathVariable Integer n2){
    try{
        Integer resull = n1/n2;
        return "Result is" + resull;
    }catch (ArithmeticException e){
       return "do compute error,cause by"+e.getMessage();
    } //try{}catch代码我们在项目中通常会认为模板代码
        //将来在项目中不建议出现大量重复得模板代码
    }

    @RequestMapping("doCompute02/{n1}/{n2}")
    public String doCompute02(@PathVariable Integer n1, @PathVariable Integer n2){
            Integer resull = n1/n2;
            return "Result is" + resull;
    }

    //在Spring中提供了一种异常处理方法
    /*
    * @ExceptionHandler注解描述的方法为控制逻辑层异常处理方法，此注解内部定义得异常类型，为处理方法可以处理得异常类型
    * 其中，异常处理方法参数中得异常类型应该与@ExceptionHandler中定义得异常类型一致
    * 或者是@ExceptionHandler注解中定义得异常类型得父类类型
    * */
    @ExceptionHandler(ArithmeticException.class)
    public String doArithmeticException(ArithmeticException e){
        return "do compute error,cause by"+e.getMessage();
    }
}
